<?
include_once("_inc/configure.php");
include_once("_class/UploadImage.class.php");
include_once("_class/db.class.php");

$db = new DB($mysql_prod_db, $mysql_prod_host, $mysql_prod_user, $mysql_prod_pass);

//mysql_pconnect('127.0.0.1', 'moka_user', 'arabica');

$type = $_POST['mimetype'];
$xhr = $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';


$query = "SELECT * FROM moka_ecommerce.negozi INNER JOIN moka_ecommerce.configurazioni ON configurazioni.id_negozio= negozi.id WHERE negozi.id='$_REQUEST[id_negozio]' ";
$rs = $db->query($query);
if(!$rs)exit($query.mysql_error());
$config = mysql_fetch_assoc($rs);
$img_fs_path = FS_K_SHOP.$config[host]."/img/";
//print_r($config);



if($_REQUEST[mod]=="del" && $_REQUEST[id_prodotto]>0 && $_REQUEST[nome_file])
{
	if(file_exists($img_fs_path."S/".$_REQUEST[nome_file]))unlink($img_fs_path."S/".$_REQUEST[nome_file]);
	if(file_exists($img_fs_path."M/".$_REQUEST[nome_file]))unlink($img_fs_path."M/".$_REQUEST[nome_file]);
	if(file_exists($img_fs_path."L/".$_REQUEST[nome_file]))unlink($img_fs_path."L/".$_REQUEST[nome_file]);
	$query = "DELETE FROM moka_ecomm_$_REQUEST[id_negozio].prodotti_immagini WHERE id_prodotto=\"$_REQUEST[id_prodotto]\" AND nome_file=\"$_REQUEST[nome_file]\" ";
	$rs = $db->query($query);
	echo mysql_error();
	$out = "[ {optionValue: 0, optionDisplay: 'Inizio'}] ";
	echo $out;
}
elseif($_POST[mimetype]=="html" && $_REQUEST[id_negozio]>0) 
{
	 
	
	// return text var_dump for the html request
	//echo "VAR DUMP:<p />";
	//var_dump($_POST);
	//var_dump($_FILES);
	

	foreach($_FILES as $file){
		 
		$n = $file['name'];
		$s = $file['size'];
		if (!$n) continue;
		$err_msg_log = null;
		$testotmp = "";

		$handle = new Upload($file);
		//print_r($_FILES['media']);
			

		if ($handle->uploaded) {
			/*if(file_exists($img_fs_path."S/".$n)&&strlen($n)>0)
			{
				unlink($img_fs_path.$n);
				@unlink($img_fs_path."S/".$n);
				@unlink($img_fs_path."M/".$n);
				@unlink($img_fs_path."L/".$n);
			}*/
			$uploaded = true;
			$tipo_med = split("/",$file[type]);
			if(!strlen($filename)>0)$img_fs_path = $img_fs_path;
			if(!file_exists($img_fs_path))
			{	
				//echo $img_fs_path;
				mkdir($img_fs_path);
			}
			
			if(!file_exists($img_fs_path."S/"))
			{	mkdir($img_fs_path."S/"); //chgrp($img_fs_path."S/","moka"); chmod($img_fs_path."S/", "g+w");
			}if(!file_exists($img_fs_path."M/"))
			{	mkdir($img_fs_path."M/"); //chgrp($img_fs_path."M/","moka"); chmod($img_fs_path."M/", "g+w");
			}if(!file_exists($img_fs_path."L/"))
			{	mkdir($img_fs_path."L/"); //chgrp($img_fs_path."L/","moka"); chmod($img_fs_path."L/", "g+w");
			}

			///// LARGE
			$handle->image_resize          = true;
			$handle->image_ratio           = true;
			$handle->image_ratio_crop		= ($config[crop_L])?true:false;
			$handle->image_y               = $config[img_L_height];
			$handle->image_x               = $config[img_L_width];

			$handle->Process($img_fs_path."L/");

			if ($handle->processed) {
				// everything was fine ! //echo '<fieldset>'; //echo '  <legend>file uploaded with success</legend>'; //echo '  <img src="test/' . $handle->file_dst_name . '" />';
				$info = getimagesize($handle->file_dst_pathname); //echo '  <p>' . $info['mime'] . ' &nbsp;-&nbsp; ' . $info[0] . ' x ' . $info[1] .' &nbsp;-&nbsp; ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>'; //echo '  link to the file just uploaded: <a href="test/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>'; //echo '</fieldset>';
			} else {
				// one error occured //echo '<fieldset>'; //echo '  <legend>file not uploaded to the wanted location</legend>'; //echo '  Error: ' . $handle->error . ''; //echo '</fieldset>';
				$err_msg_log .= "\nL ".$handle->error;
			}
			
			
			///// MEDIA
			// we now process the image a second time, with some other settings
			$handle->image_resize          = true;
			$handle->image_ratio           = true;
			$handle->image_ratio_crop		= ($config[crop_M])?true:false;
			$handle->image_y               = $config[img_M_height];
			$handle->image_x               = $config[img_M_width];
			//$img_fs_path = ROOT_SITI.$_SESSION[navigazione][nome_dir]."/media_$tipo_med[0]/med/";

			$handle->Process($img_fs_path."M/");

			// we check if everything went OK
			if ($handle->processed) {
				// everything was fine ! //echo '<fieldset>'; //echo '  <legend>file uploaded with success</legend>'; //echo '  <img src="test/' . $handle->file_dst_name . '" />';
				$info = getimagesize($handle->file_dst_pathname); //echo '  <p>' . $info['mime'] . ' &nbsp;-&nbsp; ' . $info[0] . ' x ' . $info[1] .' &nbsp;-&nbsp; ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>'; //echo '  link to the file just uploaded: <a href="test/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>'; //echo '</fieldset>';
			} else {
				// one error occured //echo '<fieldset>'; //echo '  <legend>file not uploaded to the wanted location</legend>'; //echo '  Error: ' . $handle->error . ''; //echo '</fieldset>';
				$err_msg_log .= "\nM ".$handle->error;
			}


			///// SMALL			
			$handle->image_resize          = true;
			$handle->image_ratio           = true;
			$handle->image_ratio_crop		= ($config[crop_S])?true:false;
			$handle->image_y               = $config[img_S_height];
			$handle->image_x               = $config[img_S_width];
			//$img_fs_path = ROOT_SITI.$_SESSION[navigazione][nome_dir]."/media_$tipo_med[0]/thumb/";
			//$testotmp .= "\nSMALL crop: ".$handle->image_ratio_crop;
			//$testotmp .= "\nSMALL y: ".$config[img_S_height];
			//$testotmp .= "\nSMALL x: ".$config[img_S_width];
			//$testotmp .= "\nconf x: ".print_r($config,1);
			//mail("benigni.alberto@gmail.com",__FILE__,$testotmp);

			$handle->Process($img_fs_path."S/");

			// we check if everything went OK
			if ($handle->processed) {
				// everything was fine ! //
				//echo '<fieldset>'; //echo '  <legend>file uploaded with success</legend>'; //echo '  <img src="test/' . $handle->file_dst_name . '" />';
				$info = getimagesize($handle->file_dst_pathname); //
				echo '  <p><img align="left" width="30px" src="/img/S/' . $handle->file_dst_name . '">' . $info['mime'] . ' &nbsp;-&nbsp; ' . $info[0] . ' x ' . $info[1] .' &nbsp;-&nbsp; ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB<br/>'; //
				echo '  ' . $handle->file_dst_name.'</p>' ; //
				//echo '</fieldset>';
			} else {
				// one error occured //echo '<fieldset>'; //echo '  <legend>file not uploaded to the wanted location</legend>'; //echo '  Error: ' . $handle->error . ''; //echo '</fieldset>';
				$err_msg_log .= "\nS ".$handle->error;
			}
			
			
			// MAIL IF ERROR			
			if($err_msg_log) mail("benigni.alberto@gmail.com", __FILE__, $err_msg_log);
			
			// we delete the temporary files
			$handle-> Clean();
			
			$query = "REPLACE INTO moka_ecomm_$_REQUEST[id_negozio].prodotti_immagini SET id_prodotto=\"$_REQUEST[id_prodotto]\", nome_file=\"$handle->file_dst_name\", ordine=\"99\" ";
			$rs = $db->query($query);
				
				

		} else {
			// if we're here, the upload file failed for some reasons
			// i.e. the server didn't receive the file
			$uploaded = false;
		}
			
			
			

	}
	
}
?>